/**
 * 系统参数查看和维护界面
 * @author linzongxue
 * @create_date 2012-2-14
 */
define(function(){
	var SysParamPanel = Ext.extend(Ext.grid.GridPanel, {
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    autoExpandColumn: 4,
		initComponent: function(){
			SysParamPanel.superclass.initComponent.call(Ext.apply(this, {
				store: new Ext.data.JsonStore({
				    remoteSort: true, idProperty: 'id',
				    fields: ['name', 'text', 'value', 'defaultValue', 'values', 'editable'],
				    url: 'sys/param/getAllSysParams.do'
				}),
				columns: [
					new Ext.grid.RowNumberer(),
					{
						xtype: 'actioncolumn',width: 30, items: [{
		                    icon: cu.getIconImg('edit.gif'),
		                    tooltip: '<nobr>修改参数值</nobr>',
		                    handler: function(grid, rowIndex, colIndex) {
		                    	grid.editParam(rowIndex);
		                    }
		                }]
					},
					{header: '名称', width: 120, dataIndex: 'name'},
					{header: '值', width: 200, dataIndex: 'value', editor: {allowBlank: false}},
					{header: '描述', width: 150, dataIndex: 'text'}
				]
			}));
			this.getView().on('refresh', function(view){
				//标记不可修改的参数
				Ext.each(this.store.getRange(), function(r, i){
					if (!r.get('editable')){
						view.getCell(i, 1).innerHTML = '';
						Ext.get(view.getRow(i)).setStyle('color', '#aaa');
					}
				}, this);
			}, this);
			this.on({
				afterrender: function(){
					this.refresh();
				},
				rowdblclick: function(grid, rowIndex){
					if (this.store.getAt(rowIndex).get('editable')){
						this.editParam(rowIndex);
					}
				}
			});
		},
		refresh: function(){
			this.store.load();
		},
		editParam: function(rowIndex){
			var record = this.store.getAt(rowIndex);
			if (!record.get('editable')){
				cu.alert('此参数不允许修改'); return;
			}
			use('sys.param.SysParamWindow', function(Win){
				new Win({
					paramData: record.json,
					callback: function(newValue){
						record.set('value', newValue);
						record.json.value = newValue;
						record.commit(); //去除单元格中的修改标记
						cu.hint('修改参数值成功');
					},
					cbScope: this
				}).show();
			}, this);
		}
	});
	return SysParamPanel;
});